package org.jetnuke.modules.search;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.SectionsDAO;
import org.jetnuke.dao.StoriesDAO;
import org.jetnuke.entities.Seccont;
import org.jetnuke.servlets.utilities.StringUtils;

public class DoSearch implements IAction {
	String type = "stories";

	int category;

	String author = "";

	int topicid;

	String query;

	int start = 0;

	int offset = 10;

	public void setQuery(String query) {
		this.query = StringUtils.removeHtml(query);
	}

	public void setTopicid(int topicid) {
		this.topicid = topicid;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	public void setType(String type) {
		this.type = type;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		vc.put("topics", DataAccessDriver.getInst().newTopicsDAO().selectAllOrderTopicName());// topicsPeer.doSelect(new Criteria()
				//.addAscendingOrderByColumn(topicsPeer.TOPICTEXT)));
		vc.put("cats", DataAccessDriver.getInst().newStoriesCatDAO().selectAllOrderTitle());// stories_catPeer.doSelect(new Criteria()
				//.addAscendingOrderByColumn(stories_catPeer.TITLE)));
		//vc.put("authors", DataAccessDriver.getInst().newAuthorsModel().selectAll_order_AID_asc());
		vc.put("category", new Integer(category));
		if (topicid > 0)
			vc.put("topic", DataAccessDriver.getInst().newTopicsDAO().retrieveByPK(topicid));
		//Criteria crt = new Criteria();
		int prev = start - offset;
		if (prev >= 0)
			vc.put("prev", new Integer(prev));
		vc.put("max", new Integer(start + offset));

		//crt.setLimit(offset);
		//crt.setOffset(start);

		if (type.equals("stories")) {

			//if (category > 0) {
			//	crt.add(storiesPeer.CATID, category);
//
			//}
			//if (topicid > 0) {
			//	crt.add(storiesPeer.TOPIC, topicid);
			//}

			//Criteria.Criterion a1 = crt.getNewCriterion(storiesPeer.TITLE, "%"
			//		+ query + "%", Criteria.LIKE);
			//Criteria.Criterion a2 = crt.getNewCriterion(storiesPeer.HOMETEXT,
			//		"%" + query + "%", Criteria.LIKE);
			//Criteria.Criterion a3 = crt.getNewCriterion(storiesPeer.BODYTEXT,
			//		"%" + query + "%", Criteria.LIKE);
			//Criteria.Criterion a4 = crt.getNewCriterion(storiesPeer.NOTES, "%"
			//		+ query + "%", Criteria.LIKE);

			//crt.add(a1.or(a2).or(a3).or(a4));
			//if (author.length() > 0)
			//	crt.add(storiesPeer.AID, author);
			//crt.addDescendingOrderByColumn(storiesPeer.TIME);
			List ls = null;
			StoriesDAO sm=DataAccessDriver.getInst().newStoriesDAO();
			query="%"+query+"%";
			if (author.length() > 0){
				if(category>0){
					if(topicid>0){
						ls = sm.searchOrderTimeDesc(author,category,topicid,query);
					}else{
						ls = sm.searchOrderTimeDesc2(author,category,query);
					}
				}else{
					if(topicid>0){
						ls = sm.searchOrderTimeDesc3(author,topicid,query);
					}else{
						ls = sm.searchOrderTimeDesc4(author,query);
					}
				}
			}else{
				if(category>0){
					if(topicid>0){
						ls = sm.searchOrderTimeDesc5(category,topicid,query);
					}else{
						ls = sm.searchOrderTimeDesc6(category,query);
					}
				}else{
					if(topicid>0){
						ls = sm.searchOrderTimeDesc7(topicid,query);
					}else{
						ls = sm.searchOrderTimeDesc8(query);
					}
				}
			}
			//if (query.length() > 0)
			//ls = storiesPeer.doSelect(crt);
			vc.put("stories", ls);
			return new ActionForward(info, "stories.vm");
		} else if (type.equals("comments")) {
			query="%"+query+"%";
			//DataAccessDriver.getInstance().newCommentModel().searchOrderDateDesc(query);
			//Criteria.Criterion a1 = crt.getNewCriterion(commentsPeer.SUBJECT,
				//	"%" + query + "%", Criteria.LIKE);
			//Criteria.Criterion a2 = crt.getNewCriterion(commentsPeer.COMMENT,
					//"%" + query + "%", Criteria.LIKE);
			//crt.add(a1.or(a2));
			//crt.addDescendingOrderByColumn(commentsPeer.DATE);

			List ls =DataAccessDriver.getInst().newCommentDAO().searchOrderDateDesc(query); 
				//commentsPeer.doSelect(crt);
			vc.put("comments", ls);
			return new ActionForward(info, "comments.vm");
		} else if (type.equals("reviews")) {
			query="%"+query+"%";
			//Criteria.Criterion a1 = crt.getNewCriterion(reviewsPeer.TEXT, "%"
			//		+ query + "%", Criteria.LIKE);
			//Criteria.Criterion a2 = crt.getNewCriterion(reviewsPeer.TITLE, "%"
			//		+ query + "%", Criteria.LIKE);
			//crt.add(a1.or(a2));
			//crt.addDescendingOrderByColumn(reviewsPeer.DATE);
			List ls = null;
			query="%"+query+"%";
			//if (query.length() > 0)
				ls = DataAccessDriver.getInst().newReviewsDAO().searchOrderDateDesc(query);
					//reviewsPeer.doSelect(crt);
			vc.put("reviews", ls);
			return new ActionForward(info, "reviews.vm");
		} else if (type.equals("sections")) {
			//Criteria.Criterion a1 = crt.getNewCriterion(seccontPeer.CONTENT,
			//		"%" + query + "%", Criteria.LIKE);
			//Criteria.Criterion a2 = crt.getNewCriterion(seccontPeer.TITLE, "%"
			//		+ query + "%", Criteria.LIKE);
			//crt.add(a1.or(a2));
			//crt.addDescendingOrderByColumn(seccontPeer.ARTID);
			List ls = null;
			query="%"+query+"%";
			//if (query.length() > 0) {
				ls = DataAccessDriver.getInst().newSeccontDAO().searchOrderArtidDesc(query);
					//seccontPeer.doSelect(crt);
				Iterator it = ls.iterator();
				SectionsDAO sm=DataAccessDriver.getInst().newSectionsDAO();
				while (it.hasNext()) {
					Seccont cont = (Seccont) it.next();					
					cont.setSectionTitle(sm.retrieveByPK(cont.getSECID()).getSECNAME());
				}
			//}
			vc.put("secconts", ls);
			return new ActionForward(info, "sections.vm");
		} /*else if (type.equals("users")) {
			Criteria.Criterion a1 = crt.getNewCriterion(usersPeer.NAME, "%"
					+ query + "%", Criteria.LIKE);
			Criteria.Criterion a2 = crt.getNewCriterion(usersPeer.UNAME, "%"
					+ query + "%", Criteria.LIKE);
			crt.add(a1.or(a2));
			crt.addDescendingOrderByColumn(usersPeer.UNAME);

			List ls = usersPeer.doSelect(crt);
			vc.put("users", ls);
			return new ActionForward(info, "users.vm");
		}*/
		return new ActionForward(op, info);
	}

	public void setStart(int start) {
		this.start = start;
	}
}
